<?php

namespace app\admin\service\recharge;

use app\admin\model\BoxGame;
use app\admin\model\recharge\BoxRechargeGame;
use app\admin\model\recharge\BoxRechargeOrder;
use app\admin\service\BaseService;

class BoxRechargeOrderService extends BaseService
{
    /**
     * Desc : getList
     * User : Steven
     * Date : 2024-05-28 14:00
     * @return array
     */
    public function getList(): array
    {
        $params = request()->param();
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 15;
        $where  = [];
        if (!empty($params['member_id'])) {
            $where[] = ['a.member_id', '=', $params['member_id']];
        }
        if (!empty($params['game_id'])) {
            $where[] = ['a.game_id', '=', $params['game_id']];
        }
        if (!empty($params['nickname'])) {
            $where[] = ['b.nickname', 'like', '%' . $params['nickname'] . '%'];
        }
        if (!empty($params['order_id'])) {
            $where[] = ['a.order_num', '=', $params['order_id']];
        }
        if (isset($params['send_status']) && is_numeric($params['send_status'])) {
            $where[] = ['a.send_status', '=', $params['send_status']];
        }
        $pay_status = $params['pay_status'] ?? "";
        if ($pay_status != '') {
            $where[] = ['a.pay_status', '=', $pay_status];
        }
        if (!empty($params['platform'])) {
            $where[] = ['a.platform', '=', $params['platform']];
        }
        if (!empty($params['time_range'][0]) && !empty($params['time_range'][1])) {
            $where[] = ['a.create_time', 'between', [$params['time_range'][0], $params['time_range'][1]]];
        }
        $model = new BoxRechargeOrder();
        $arr = $model->alias('a')
            ->leftJoin('cq_member b', 'a.member_id = b.id')
            ->field('a.*,b.nickname')
            ->where($where)->order('id','desc')->paginate($limit)->toArray();

        $game_ids = (new BoxRechargeGame())->column( 'game_id');
        $game_list = (new BoxGame())->whereIn('id', $game_ids)->column('game_name', 'id');
        array_walk($arr['data'], function (&$v) use ($game_list) {
          $v['game_name'] = $game_list[$v['game_id']] ?? '';
        });

        return [
            'code' => 0,
            'msg' => 'success',
            'count' => $arr['total'],
            'data' => $arr['data']
        ];
    }
}
